package com.youi.third.push;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.MultiMedia;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.youi.util.JsonUtil;
import com.youi.util.LogUtil;

import java.io.IOException;

/**
 * 个推推送工具
 * @author erdong.Chan
 *
 */
public class GetuiPushUtil {

	//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
    private static String appId = "2f2yC7DnkA7VoQjsCqowA4";
    private static String appKey = "rUYqm5YRBY9XpR8CZUkUH";
    private static String masterSecret = "NLtx9bb6496iRj6ED0byg9";

    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    		
    public static void main(String[] args) throws IOException {
    	PushtoSingle("f382f97a8dcabab435c739b890a7af16", JsonUtil.toJson(new PushEntity(0, null, "duang")),"duang");
    }
    
    /**
     * 推送
     * @param token
     * @param content
     * @param alertMsg
     * @return
     */
    public static boolean PushtoSingle(String token,String content,String alertMsg){
    	if (token!=null) {
			if (token.startsWith("1")) {
				//推送到ios
		    	PushtoIOSSingle(token, content, alertMsg);
			}else if (token.startsWith("0")){
				//推送到安卓
		    	PushtoAndroidSingle(token, content);
			}else {
				//两边都推
				PushtoIOSSingle(token, content, alertMsg);
				PushtoAndroidSingle(token, content);
			}
		}
    	return true;
    }
    
    /**
     * 推送至安卓
     * @param token
     * @param content
     * @return
     */
    public static boolean PushtoAndroidSingle(String token,String content){
    	 IGtPush push = new IGtPush(url, appKey, masterSecret);
    	 TransmissionTemplate template = transmissionTemplateDemo(content);
         SingleMessage message = new SingleMessage();
         message.setOffline(true);
         // 离线有效时间，单位为毫秒，可选
         message.setOfflineExpireTime(24 * 3600 * 1000);
         message.setData(template);
         // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
         message.setPushNetWorkType(0);
         Target target = new Target();
         target.setAppId(appId);
         // target.setClientId(CID);
         target.setAlias(token);
         IPushResult ret = null;
         try {
             ret = push.pushMessageToSingle(message, target);
         } catch (RequestException e) {
             e.printStackTrace();
             ret = push.pushMessageToSingle(message, target, e.getRequestId());
         }
         if (ret != null) {
             System.out.println(ret.getResponse().toString());
             LogUtil.Info(ret.getResponse().toString());
             if (ret.getResponse().toString().contains("result=ok")) {
				return true;
			 }
         } else {
             System.out.println("服务器响应异常");
         }
    	return false ;
    }
    
    /**
     * 推送至ios
     * @param token
     * @param content
     * @return
     */
    public static boolean PushtoIOSSingle(String token,String content,String alertMsg){
   	 	IGtPush push = new IGtPush(url, appKey, masterSecret);
   	 	TransmissionTemplate template = transmissionTemplateIOSDemo(content,alertMsg);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        // target.setClientId(CID);
        target.setAlias(token);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
            LogUtil.Info(ret.getResponse().toString());
            if (ret.getResponse().toString().contains("result=ok")) {
				return true;
			 }
        } else {
            System.out.println("服务器响应异常");
        }
        return false ;
   }
    
    
	
    /**
     * 默认安卓demo
     * @param content
     * @return
     */
    public static TransmissionTemplate transmissionTemplateDemo(String content) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(content);   
        return template;
    }
    
    /**
     * ios demo
     * @param content
     * @return
     */
    public static TransmissionTemplate transmissionTemplateIOSDemo(String content,String alertMsg) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(content);
        APNPayload payload = new APNPayload();
        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(alertMsg));   
        template.setAPNInfo(payload); 
        return template;
    }
	
}
